/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package l2s.gameserver.scripts.quests;

import l2s.commons.util.Rnd;
import l2s.gameserver.ai.CtrlEvent;
import l2s.gameserver.model.L2World;
import l2s.gameserver.model.actor.L2Npc;
import l2s.gameserver.model.actor.instance.L2PcInstance;
import l2s.gameserver.model.itemcontainer.Inventory;
import l2s.gameserver.model.quest.Quest;
import l2s.gameserver.model.quest.QuestState;
import l2s.gameserver.model.quest.QuestTimer;
import l2s.gameserver.scripting.ScriptFile;

public class Q229_TestOfWitchcraft extends Quest implements ScriptFile
{
	private static final String qn = "Q229_TestOfWitchcraft";
	private static final int Orim = 30630;
	private static final int Alexandria = 30098;
	private static final int Iker = 30110;
	private static final int Kaira = 30476;
	private static final int Lara = 30063;
	private static final int Roderik = 30631;
	private static final int Nestle = 30314;
	private static final int Leopold = 30435;
	private static final int Vasper = 30417;
	private static final int Vadin = 30188;
	private static final int Evert = 30633;
	private static final int Endrigo = 30632;
	private static final int MarkOfWitchcraft = 3307;
	private static final int OrimsDiagram = 3308;
	private static final int AlexandriasBook = 3309;
	private static final int IkersList = 3310;
	private static final int DireWyrmFang = 3311;
	private static final int LetoLizardmanCharm = 3312;
	private static final int EnchantedGolemHeartstone = 3313;
	private static final int LarasMemo = 3314;
	private static final int NestlesMemo = 3315;
	private static final int LeopoldsJournal = 3316;
	private static final int Aklantoth_1stGem = 3317;
	private static final int Aklantoth_2stGem = 3318;
	private static final int Aklantoth_3stGem = 3319;
	private static final int Aklantoth_4stGem = 3320;
	private static final int Aklantoth_5stGem = 3321;
	private static final int Aklantoth_6stGem = 3322;
	private static final int Brimstone_1st = 3323;
	private static final int OrimsInstructions = 3324;
	private static final int Orims1stLetter = 3325;
	private static final int Orims2stLetter = 3326;
	private static final int SirVaspersLetter = 3327;
	private static final int VadinsCrucifix = 3328;
	private static final int TamlinOrcAmulet = 3329;
	private static final int VadinsSanctions = 3330;
	private static final int IkersAmulet = 3331;
	private static final int SoultrapCrystal = 3332;
	private static final int PurgatoryKey = 3333;
	private static final int ZeruelBindCrystal = 3334;
	private static final int Brimstone_2nd = 3335;
	private static final int SwordOfBinding = 3029;
	private static final int DireWyrm = 20557;
	private static final int EnchantedStoneGolem = 20565;
	private static final int LetoLizardman = 20577;
	private static final int LetoLizardmanArcher = 20578;
	private static final int LetoLizardmanSoldier = 20579;
	private static final int LetoLizardmanWarrior = 20580;
	private static final int LetoLizardmanShaman = 20581;
	private static final int LetoLizardmanOverlord = 20582;
	private static final int NamelessRevenant = 27099;
	private static final int SkeletalMercenary = 27100;
	private static final int DrevanulPrinceZeruel = 27101;
	private static final int TamlinOrc = 20601;
	private static final int TamlinOrcArcher = 20602;
	private static final int[][] DROPLIST_COND =
	{
		{
			2,
			0,
			DireWyrm,
			IkersList,
			DireWyrmFang,
			20,
			100,
			1
		},
		{
			2,
			0,
			EnchantedStoneGolem,
			IkersList,
			EnchantedGolemHeartstone,
			20,
			80,
			1
		},
		{
			2,
			0,
			LetoLizardman,
			IkersList,
			LetoLizardmanCharm,
			20,
			50,
			1
		},
		{
			2,
			0,
			LetoLizardmanArcher,
			IkersList,
			LetoLizardmanCharm,
			20,
			50,
			1
		},
		{
			2,
			0,
			LetoLizardmanSoldier,
			IkersList,
			LetoLizardmanCharm,
			20,
			60,
			1
		},
		{
			2,
			0,
			LetoLizardmanWarrior,
			IkersList,
			LetoLizardmanCharm,
			20,
			60,
			1
		},
		{
			2,
			0,
			LetoLizardmanShaman,
			IkersList,
			LetoLizardmanCharm,
			20,
			70,
			1
		},
		{
			2,
			0,
			LetoLizardmanOverlord,
			IkersList,
			LetoLizardmanCharm,
			20,
			70,
			1
		},
		{
			2,
			0,
			NamelessRevenant,
			LarasMemo,
			Aklantoth_3stGem,
			1,
			100,
			1
		},
		{
			6,
			0,
			TamlinOrc,
			VadinsCrucifix,
			TamlinOrcAmulet,
			20,
			50,
			1
		},
		{
			6,
			0,
			TamlinOrcArcher,
			VadinsCrucifix,
			TamlinOrcAmulet,
			20,
			55,
			1
		}
	};
	private static boolean QuestProf = true;
	
	public Q229_TestOfWitchcraft(int questId, String name, String descr)
	{
		super(questId, name, descr);
		addStartNpc(Orim);
		addTalkId(Alexandria);
		addTalkId(Iker);
		addTalkId(Kaira);
		addTalkId(Lara);
		addTalkId(Roderik);
		addTalkId(Nestle);
		addTalkId(Leopold);
		addTalkId(Vasper);
		addTalkId(Vadin);
		addTalkId(Evert);
		addTalkId(Endrigo);
		for (int[] element : DROPLIST_COND)
		{
			addKillId(element[2]);
		}
		addKillId(SkeletalMercenary);
		addKillId(DrevanulPrinceZeruel);
		questItemIds = new int[]
		{
			OrimsDiagram,
			OrimsInstructions,
			Orims1stLetter,
			Orims2stLetter,
			Brimstone_1st,
			AlexandriasBook,
			IkersList,
			Aklantoth_1stGem,
			SoultrapCrystal,
			IkersAmulet,
			Aklantoth_2stGem,
			LarasMemo,
			NestlesMemo,
			LeopoldsJournal,
			Aklantoth_4stGem,
			Aklantoth_5stGem,
			Aklantoth_6stGem,
			SirVaspersLetter,
			SwordOfBinding,
			VadinsCrucifix,
			VadinsSanctions,
			Brimstone_2nd,
			PurgatoryKey,
			ZeruelBindCrystal,
			DireWyrmFang,
			EnchantedGolemHeartstone,
			LetoLizardmanCharm,
			Aklantoth_3stGem,
			TamlinOrcAmulet
		};
	}
	
	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		String htmltext = event;
		QuestState st = player.getQuestState(qn);
		if (event.equalsIgnoreCase("30630-08.htm"))
		{
			st.playSound(QuestState.SOUND_ACCEPT);
			st.giveItems(OrimsDiagram, 1);
			st.giveItems(7562, 64);
			st.set("cond", "1");
			st.setState(Quest.STATE_STARTED);
		}
		else if (event.equalsIgnoreCase("30098-03.htm"))
		{
			st.giveItems(AlexandriasBook, 1);
			st.takeItems(OrimsDiagram, 1);
			st.set("cond", "2");
			st.setState(Quest.STATE_STARTED);
		}
		else if (event.equalsIgnoreCase("30110-03.htm"))
		{
			st.giveItems(IkersList, 1);
		}
		else if (event.equalsIgnoreCase("30476-02.htm"))
		{
			st.giveItems(Aklantoth_2stGem, 1);
		}
		else if (event.equalsIgnoreCase("30063-02.htm"))
		{
			st.giveItems(LarasMemo, 1);
		}
		else if (event.equalsIgnoreCase("30314-02.htm"))
		{
			st.giveItems(NestlesMemo, 1);
		}
		else if (event.equalsIgnoreCase("30435-02.htm"))
		{
			st.takeItems(NestlesMemo, 1);
			st.giveItems(LeopoldsJournal, 1);
		}
		else if (event.equalsIgnoreCase("30630-14.htm"))
		{
			L2Npc isQuest = L2World.getInstance().getNpcById(DrevanulPrinceZeruel);
			if (isQuest != null)
			{
				htmltext = "30630-fail.htm";
			}
			else
			{
				st.takeItems(AlexandriasBook, 1);
				st.takeItems(Aklantoth_1stGem, 1);
				st.takeItems(Aklantoth_2stGem, 1);
				st.takeItems(Aklantoth_3stGem, 1);
				st.takeItems(Aklantoth_4stGem, 1);
				st.takeItems(Aklantoth_5stGem, 1);
				st.takeItems(Aklantoth_6stGem, 1);
				if (st.getQuestItemsCount(Brimstone_1st) == 0)
				{
					st.giveItems(Brimstone_1st, 1);
				}
				st.set("cond", "4");
				st.set("id", "1");
				st.setState(Quest.STATE_STARTED);
				addSpawn(DrevanulPrinceZeruel, player, false, 0, true);
				startQuestTimer("DrevanulPrinceZeruel_Fail", 300000, npc, player, false);
				L2Npc Zeruel = addSpawn(DrevanulPrinceZeruel, player, false, 0, true);
				if (Zeruel != null)
				{
					Zeruel.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, st.getPlayer(), 1);
				}
			}
		}
		else if (event.equalsIgnoreCase("30630-16.htm"))
		{
			htmltext = "30630-16.htm";
			st.takeItems(Brimstone_1st, -1);
			st.giveItems(OrimsInstructions, 1);
			st.giveItems(Orims1stLetter, 1);
			st.giveItems(Orims2stLetter, 1);
			st.set("cond", "6");
		}
		else if (event.equalsIgnoreCase("30110-08.htm"))
		{
			st.takeItems(Orims2stLetter, 1);
			st.giveItems(SoultrapCrystal, 1);
			st.giveItems(IkersAmulet, 1);
			if (st.getQuestItemsCount(SwordOfBinding) > 0)
			{
				st.set("cond", "7");
				st.setState(Quest.STATE_STARTED);
			}
		}
		else if (event.equalsIgnoreCase("30417-03.htm"))
		{
			st.takeItems(Orims1stLetter, 1);
			st.giveItems(SirVaspersLetter, 1);
		}
		else if (event.equalsIgnoreCase("30633-02.htm"))
		{
			L2Npc isQuest = L2World.getInstance().getNpcById(DrevanulPrinceZeruel);
			if (isQuest != null)
			{
				htmltext = "30633-fail.htm";
			}
			else
			{
				st.set("id", "2");
				st.set("cond", "9");
				if (st.getQuestItemsCount(Brimstone_2nd) == 0)
				{
					st.giveItems(Brimstone_2nd, 1);
				}
				addSpawn(DrevanulPrinceZeruel, player, false, 0, true);
				startQuestTimer("DrevanulPrinceZeruel_Fail", 300000, npc, player, false);
			}
		}
		else if (event.equalsIgnoreCase("30630-20.htm"))
		{
			st.takeItems(ZeruelBindCrystal, 1);
		}
		else if (event.equalsIgnoreCase("30630-21.htm"))
		{
			st.takeItems(PurgatoryKey, 1);
		}
		else if (event.equalsIgnoreCase("30630-22.htm"))
		{
			st.takeItems(SwordOfBinding, -1);
			st.takeItems(IkersAmulet, -1);
			st.takeItems(OrimsInstructions, -1);
			st.getPlayer().addExpAndSp(139796, 40000);
			st.giveItems(MarkOfWitchcraft, 1);
			st.playSound(QuestState.SOUND_FINISH);
			st.exitQuest(true);
		}
		if (event.equalsIgnoreCase("DrevanulPrinceZeruel_Fail"))
		{
			L2Npc isQuest = L2World.getInstance().getNpcById(DrevanulPrinceZeruel);
			if (isQuest != null)
			{
				isQuest.deleteMe();
			}
		}
		return htmltext;
	}
	
	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		QuestState st = player.getQuestState(qn);
		String htmltext = getNoQuestMsg();
		int npcId = npc.getNpcId();
		int cond = st.getInt("cond");
		if (npcId == Orim)
		{
			if (st.getQuestItemsCount(MarkOfWitchcraft) != 0)
			{
				htmltext = getAlreadyCompletedMsg();
				st.exitQuest(true);
			}
			else if (cond == 0)
			{
				if ((st.getPlayer().getClassId().getId() == 0x0b) || (st.getPlayer().getClassId().getId() == 0x04) || (st.getPlayer().getClassId().getId() == 0x20))
				{
					if (st.getPlayer().getLevel() < 39)
					{
						htmltext = "30630-02.htm";
						st.exitQuest(true);
					}
					else if (st.getPlayer().getClassId().getId() == 0x0b)
					{
						htmltext = "30630-03.htm";
					}
					else
					{
						htmltext = "30630-05.htm";
					}
				}
				else
				{
					htmltext = "30630-01.htm";
					st.exitQuest(true);
				}
			}
			else if (cond == 1)
			{
				htmltext = "30630-09.htm";
			}
			else if (cond == 2)
			{
				htmltext = "30630-10.htm";
			}
			else if ((cond == 3) || (st.getInt("id") == 1))
			{
				htmltext = "30630-11.htm";
			}
			else if (cond == 5)
			{
				htmltext = "30630-15.htm";
			}
			else if (cond == 6)
			{
				htmltext = "30630-17.htm";
			}
			else if (cond == 7)
			{
				htmltext = "30630-18.htm";
				st.set("cond", "8");
			}
			else if (cond == 10)
			{
				if (st.getQuestItemsCount(ZeruelBindCrystal) != 0)
				{
					htmltext = "30630-19.htm";
				}
				else if (st.getQuestItemsCount(PurgatoryKey) != 0)
				{
					htmltext = "30630-20.htm";
				}
				else
				{
					htmltext = "30630-21.htm";
				}
			}
		}
		else if (npcId == Alexandria)
		{
			if (cond == 1)
			{
				htmltext = "30098-01.htm";
			}
			else if (cond == 2)
			{
				htmltext = "30098-04.htm";
			}
			else
			{
				htmltext = "30098-05.htm";
			}
		}
		else if (npcId == Iker)
		{
			if (cond == 2)
			{
				if ((st.getQuestItemsCount(Aklantoth_1stGem) == 0) && (st.getQuestItemsCount(IkersList) == 0))
				{
					htmltext = "30110-01.htm";
				}
				else if ((st.getQuestItemsCount(IkersList) > 0) && ((st.getQuestItemsCount(DireWyrmFang) < 20) || (st.getQuestItemsCount(LetoLizardmanCharm) < 20) || (st.getQuestItemsCount(EnchantedGolemHeartstone) < 20)))
				{
					htmltext = "30110-04.htm";
				}
				else if ((st.getQuestItemsCount(Aklantoth_1stGem) == 0) && (st.getQuestItemsCount(IkersList) > 0))
				{
					st.takeItems(IkersList, -1);
					st.takeItems(DireWyrmFang, -1);
					st.takeItems(LetoLizardmanCharm, -1);
					st.takeItems(EnchantedGolemHeartstone, -1);
					st.giveItems(Aklantoth_1stGem, 1);
					htmltext = "30110-05.htm";
				}
				else if (st.getQuestItemsCount(Aklantoth_1stGem) == 1)
				{
					htmltext = "30110-06.htm";
				}
			}
			else if (cond == 6)
			{
				htmltext = "30110-07.htm";
			}
			else if (cond == 10)
			{
				htmltext = "30110-10.htm";
			}
			else
			{
				htmltext = "30110-09.htm";
			}
		}
		else if (npcId == Kaira)
		{
			if (cond == 2)
			{
				if (st.getQuestItemsCount(Aklantoth_2stGem) == 0)
				{
					htmltext = "30476-01.htm";
				}
				else
				{
					htmltext = "30476-03.htm";
				}
			}
			else if (cond > 2)
			{
				htmltext = "30476-04.htm";
			}
		}
		else if (npcId == Lara)
		{
			if (cond == 2)
			{
				if ((st.getQuestItemsCount(LarasMemo) == 0) && (st.getQuestItemsCount(Aklantoth_3stGem) == 0))
				{
					htmltext = "30063-01.htm";
				}
				else if ((st.getQuestItemsCount(LarasMemo) == 1) && (st.getQuestItemsCount(Aklantoth_3stGem) == 0))
				{
					htmltext = "30063-03.htm";
				}
				else if (st.getQuestItemsCount(Aklantoth_3stGem) == 1)
				{
					htmltext = "30063-04.htm";
				}
			}
			else if (cond > 2)
			{
				htmltext = "30063-05.htm";
			}
		}
		else if ((npcId == Roderik) && (cond == 2) && (st.getQuestItemsCount(LarasMemo) > 0))
		{
			htmltext = "30631-01.htm";
		}
		else if ((npcId == Nestle) && (cond == 2))
		{
			if ((st.getQuestItemsCount(Aklantoth_1stGem) > 0) && (st.getQuestItemsCount(Aklantoth_2stGem) > 0) && (st.getQuestItemsCount(Aklantoth_3stGem) > 0))
			{
				htmltext = "30314-01.htm";
			}
			else
			{
				htmltext = "30314-04.htm";
			}
		}
		else if (npcId == Leopold)
		{
			if ((cond == 2) && (st.getQuestItemsCount(NestlesMemo) > 0))
			{
				if ((st.getQuestItemsCount(Aklantoth_4stGem) + st.getQuestItemsCount(Aklantoth_5stGem) + st.getQuestItemsCount(Aklantoth_6stGem)) == 0)
				{
					htmltext = "30435-01.htm";
				}
				else
				{
					htmltext = "30435-04.htm";
				}
			}
			else
			{
				htmltext = "30435-05.htm";
			}
		}
		else if (npcId == Vasper)
		{
			if (cond == 6)
			{
				if ((st.getQuestItemsCount(SirVaspersLetter) > 0) || (st.getQuestItemsCount(VadinsCrucifix) > 0))
				{
					htmltext = "30417-04.htm";
				}
				else if (st.getQuestItemsCount(VadinsSanctions) == 0)
				{
					htmltext = "30417-01.htm";
				}
				else if (st.getQuestItemsCount(VadinsSanctions) != 0)
				{
					htmltext = "30417-05.htm";
					st.takeItems(VadinsSanctions, 1);
					st.giveItems(SwordOfBinding, 1);
					if (st.getQuestItemsCount(SoultrapCrystal) > 0)
					{
						st.set("cond", "7");
						st.setState(Quest.STATE_STARTED);
					}
				}
			}
			else if (cond == 7)
			{
				htmltext = "30417-06.htm";
			}
		}
		else if (npcId == Vadin)
		{
			if (cond == 6)
			{
				if (st.getQuestItemsCount(SirVaspersLetter) != 0)
				{
					htmltext = "30188-01.htm";
					st.takeItems(SirVaspersLetter, 1);
					st.giveItems(VadinsCrucifix, 1);
				}
				else if ((st.getQuestItemsCount(VadinsCrucifix) > 0) && (st.getQuestItemsCount(TamlinOrcAmulet) < 20))
				{
					htmltext = "30188-02.htm";
				}
				else if (st.getQuestItemsCount(TamlinOrcAmulet) >= 20)
				{
					htmltext = "30188-03.htm";
					st.takeItems(TamlinOrcAmulet, -1);
					st.takeItems(VadinsCrucifix, -1);
					st.giveItems(VadinsSanctions, 1);
				}
				else if (st.getQuestItemsCount(VadinsSanctions) > 0)
				{
					htmltext = "30188-04.htm";
				}
			}
			else if (cond == 7)
			{
				htmltext = "30188-05.htm";
			}
		}
		else if (npcId == Evert)
		{
			if ((st.getInt("id") == 2) || ((cond == 8) && (st.getQuestItemsCount(Brimstone_2nd) == 0)))
			{
				htmltext = "30633-01.htm";
			}
			else
			{
				htmltext = "30633-03.htm";
			}
		}
		else if ((npcId == Endrigo) && (cond == 2))
		{
			htmltext = "30632-01.htm";
		}
		return htmltext;
	}
	
	@Override
	public String onKill(L2Npc npc, L2PcInstance player, boolean isPet)
	{
		int npcId = npc.getNpcId();
		QuestState st = checkPlayerState(player, npc, STATE_STARTED);
		if (st == null)
		{
			return null;
		}
		int cond = st.getInt("cond");
		for (int[] element : DROPLIST_COND)
		{
			if ((cond == element[0]) && (npcId == element[2]))
			{
				if ((element[3] == 0) || (st.getQuestItemsCount(element[3]) > 0))
				{
					if (npcId == NamelessRevenant)
					{
						st.takeItems(LarasMemo, -1);
					}
					if (element[5] == 0)
					{
						st.rollAndGive(element[4], element[7], element[6], QuestProf);
					}
					else if (st.rollAndGive(element[4], element[7], element[7], element[5], element[6], QuestProf))
					{
						if ((element[1] != cond) && (element[1] != 0))
						{
							st.set("cond", String.valueOf(element[1]));
							st.setState(Quest.STATE_STARTED);
						}
					}
				}
			}
		}
		if ((cond == 2) && (st.getQuestItemsCount(LeopoldsJournal) > 0) && (npcId == SkeletalMercenary))
		{
			if ((st.getQuestItemsCount(Aklantoth_4stGem) == 0) && Rnd.chance(50))
			{
				st.giveItems(Aklantoth_4stGem, 1);
			}
			if ((st.getQuestItemsCount(Aklantoth_5stGem) == 0) && Rnd.chance(50))
			{
				st.giveItems(Aklantoth_5stGem, 1);
			}
			if ((st.getQuestItemsCount(Aklantoth_6stGem) == 0) && Rnd.chance(50))
			{
				st.giveItems(Aklantoth_6stGem, 1);
			}
			if ((st.getQuestItemsCount(Aklantoth_4stGem) != 0) && (st.getQuestItemsCount(Aklantoth_5stGem) != 0) && (st.getQuestItemsCount(Aklantoth_6stGem) != 0))
			{
				st.takeItems(LeopoldsJournal, -1);
				st.playSound(QuestState.SOUND_MIDDLE);
				st.set("cond", "3");
				st.setState(Quest.STATE_STARTED);
			}
		}
		else if ((cond == 4) && (npcId == DrevanulPrinceZeruel))
		{
			QuestTimer timer = getQuestTimer("DrevanulPrinceZeruel_Fail", npc, player);
			if (timer != null)
			{
				timer.cancel();
			}
			L2Npc isQuest = L2World.getInstance().getNpcById(DrevanulPrinceZeruel);
			if (isQuest != null)
			{
				isQuest.deleteMe();
			}
			st.set("cond", "5");
			st.unset("id");
			st.setState(Quest.STATE_STARTED);
		}
		else if ((cond == 9) && (npcId == DrevanulPrinceZeruel))
		{
			if (st.getItemEquipped(Inventory.PAPERDOLL_RHAND) == SwordOfBinding)
			{
				st.takeItems(Brimstone_2nd, 1);
				st.takeItems(SoultrapCrystal, 1);
				st.giveItems(PurgatoryKey, 1);
				st.giveItems(ZeruelBindCrystal, 1);
				st.playSound(QuestState.SOUND_MIDDLE);
				st.unset("id");
				st.set("cond", "10");
				st.setState(Quest.STATE_STARTED);
				return "You trapped the Seal of Drevanul Prince Zeruel";
			}
			QuestTimer timer = getQuestTimer("DrevanulPrinceZeruel_Fail", npc, player);
			if (timer != null)
			{
				timer.cancel();
			}
			L2Npc isQuest = L2World.getInstance().getNpcById(DrevanulPrinceZeruel);
			if (isQuest != null)
			{
				isQuest.deleteMe();
			}
		}
		return null;
	}
	
	public static void onLoad()
	{
		new Q229_TestOfWitchcraft(229, qn, "Test Of The Witchcraft");
	}
}
